#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/25 下午 12:01
# @Author  : wasdzy
# @Email   :
# @File    : __init__.py.py
# @Software: PyCharm
import requests
from bs4 import BeautifulSoup
import sys
import json
import time, codecs
import lxml

# 获取系统编码
type = sys.getfilesystemencoding()
# header信息
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    'Origin': 'https://cd.ke.com',
    'Referer': 'https://cd.ke.com/',
    'Upgrade-Insecure-Requests': '1',
}


def main(urlPath):
    result = requests.get(urlPath, headers=headers)
    # 设置结果编码
    result.encoding = 'utf-8'
    if result.status_code == 200:
        result_content = result.content.decode(type)
        rs = BeautifulSoup(result_content, 'lxml')
        # 查找到一页中所有的item 数据
        rs_a = rs.find_all('a', href=True, attrs={'class': 'CLICKDATA maidian-detail'})
        try:
            for item in rs_a:
                # 查找一页的数据
                detailUrl = (item.get("href"))
                getDetail(detailUrl)
        except Exception as e:
            print(e)


# 获取一页数据并解析数据
def getDetail(detailUrl):
    result = requests.get(detailUrl, headers=headers)
    result.encoding = 'utf-8'
    start = detailUrl.rindex('/') + 1
    end = detailUrl.rindex('.html')
    id = detailUrl[start:end]
    if result.status_code == 200:
        result_content = result.content.decode(type)
        rs = BeautifulSoup(result_content, 'lxml')
        # 解析html数据
        rsPrice = rs.find('span', attrs={'class': 'total'})
        rsUnit = rs.find('span', attrs={'class': 'unit'})
        realUnit = rsUnit.find('span')
        totalPrice = '%s%s' % (rsPrice.text, realUnit.text)
        title = rs.find('h1', attrs={'class': 'main'}).text
        acreage = rs.find('div', attrs={'class': 'area'}).find('div', attrs={'class': 'mainInfo'}).text
        housetype = rs.find('div', attrs={'class': 'room'}).find('div', attrs={'class': 'mainInfo'}).text
        # 输出数据
        print('【房屋Id：%s 标题：%s 总价：%s 面积：%s 户型：%s】' % (id, title, totalPrice, acreage, housetype))


def start(name):
    # 生成分页数据
    for i in range(0, 8):
        i = i + 1
        if i == 1:
            main('https://cd.ke.com/ershoufang/rs%s/' % name)
        else:
            main('https://cd.ke.com/ershoufang/pg%srs%s/' % (i, name))


# 入口
if __name__ == '__main__':
    start('小区名字')